<?php
    require APPPATH .'third_party/admin_controller'.EXT;
    class Home extends Admin_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }
        public function index()
        {
			$this->check_login_admin();
            $data = array();
            $this->title = "Home";  
            
		
            $this->load_render($this->load->view('admin/home',$data,TRUE));
            
        }
        
        public function register()
        {
            $data = array(
				'username' => $this->input->post('username',true),
				'password' => md5($this->input->post('password',true)),
				'email'    => 'nongtinh3@gmail.com',
				'level'    => '1',
				'time_in'  => date('Y-m-d H:i:s'),
				'time_out' => date('Y-m-d H:i:s'),
				'url_key'  => random_string('alnum', 40),
				'key'      => 'U'.$this->db->insert_id()
			);		   
			$result = $this->users->insert_user($data);
			if(isset($result))
			{
				echo  1;
			}
			else
			{
				echo  2;
			}		
        }
		public function add_item()
		{
			$this->check_login_admin();
			$data = array();
            $email    = $this->input->post('txt_email',true);            
            $image 					= $this->input->post('image',true);
			$image_thumb 			= $this->input->post('image_thumb',true); 
            $name = isset($_FILES['txt_upload']['name']) ? $_FILES['txt_upload']['name'] : "";  
            $id = $this->input->post('id_ctv');
             // Kiem tra Email co ton tai hay chua
            $data = array(
                'fullname' => $this->input->post('txt_fullname',true),
                'username' => $this->input->post('txt_user',true),
                'password' => md5($this->input->post('txt_repass',true)),
                'email'    => $this->input->post('txt_email',true),
                'level'    => $this->input->post('txt_phanquyen',true),
                'phone'    => $this->input->post('txt_phone',true),
                'address'  => $this->input->post('txt_address',true),
                'country'  => $this->input->post('txt_country',true),
                'avarta'   => $name,
                'info'     => $this->input->post('txt_info',true),
                'gender'   => $this->input->post('txt_gender',true),
                'time_in'  => date('Y-m-d H:i:s'),
                'time_out' => date('Y-m-d H:i:s'),
                'birthday' => $this->input->post('txt_birthday',true)               
            );
            if($name !='')
            {
                    $config['upload_path'] = './upload/avarta/';
					$config['allowed_types'] = 'gif|jpg|png';
					$config['remove_spaces'] = TRUE;
					$config['max_size'] = 1024;
					$config['max_width'] = '1024';
					$config['max_height'] = '1024';
					$config['file_name'] = $_FILES['txt_upload']['name'];
					$this->load->library('upload', $config);
                    if(!$this->upload->do_upload('txt_upload')) 
					{
					return false;
					} else {
		            $upload = $this->upload->data();
					$data['image'] = 'upload/avarta/'.$upload['file_name'];
                    }                    
            }
            else
            {
                if($image !='' && $image_thumb !='') {
						$data['image'] = $image;
						$data['image_thumb'] = $image_thumb;
						} else {
							$data['image'] = '';
							$data['image_thumb'] = '';
						}
            }
             if($id!='')
             {
                if($this->ctv->update_ctv($id,$data))
                {
                    $data['error'] = "Cập nhật thành công";
                   
                }
                 redirect('admin/home');
             }  
             else
             {
                if($this->ctv->create_ctv($data))
                {
                    $data['error'] = "Thêm thành công";
                     
                }
                 redirect('admin/home');
             }      
            $data['list_country'] = $this->mains->list_country();
            $this->load_render($this->load->view('members/add_item',$data,TRUE));
		
			
		}
		function logout()
		{		
		  $this->session->sess_destroy();
			 $data_user = array(
                        'username' => '',
                        'fullname' => '',
                        'id'       =>'',
                        'logged_in'       => FALSE
                    );
					$this->session->unset_userdata($data_user);
			redirect('admin/login/');
		}
    }
?>